package de.pharmacontrol.psa.beans;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.ejb.Singleton;

import de.pharmacontrol.pilot.entities.Line;
import de.pharmacontrol.pilot.entities.LogEntry;
import de.pharmacontrol.pilot.entities.LogEntry.Type;
import de.pharmacontrol.pilot.entities.User;
import de.pharmacontrol.psa.beans.interfaces.LogEntryService;
import de.pharmacontrol.psa.beans.interfaces.LoggerService;

@Singleton
public class LoggerServiceBean implements LoggerService {

	@EJB
	LogEntryService logBean;
	
	private Line line = null;
	private User user = null;

	public Line getLine() {
		return line;
	}

	public void setLine(Line line) {
		this.line = line;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	@Override
	public void debug(String message) {
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).fine(message);
	}

	@Override
	public void info(String message) {
		logBean.createLogEntry(message, LogEntry.Type.INFO, user, line);

	}

	@Override
	public void warn(String message) {
		logBean.createLogEntry(message, LogEntry.Type.WARNING, user, line);

	}

	@Override
	public void error(String message, Throwable e) {
		logBean.createLogEntry(message, LogEntry.Type.ERROR, user, line);
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, message, e);

	}

	@Override
	public void fatal(String message, Throwable e) {
		logBean.createLogEntry(message, LogEntry.Type.FATAL, user, line);
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, message, e);

	}

	@Override
	public void log(Type type, String message) {
		logBean.createLogEntry(message, type, user, line);

	}

	@Override
	public void log(Type type, String message, Throwable e) {
		logBean.createLogEntry(message, type, user, line);
		if (type == Type.ERROR || type == Type.FATAL) {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, message, e);
		}
	}

	@Override
	public void log(Type type, Line l, User u, String message, Throwable e) {
		logBean.createLogEntry(message, type, u, l);
		if (type == Type.ERROR || type == Type.FATAL) {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, message, e);
		}

	}

}
